﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.DataControls;

public partial class Templates_PressReleaseClippingsHolder_Default : Acms.AcmsControls.TemplatePage

{
    private string topicLanguage;
    private string topicProduct;

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsList.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsList_SelectData);
        dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.AcmsDataListItemEventArgs>(dlFeed_ItemDataBound);
        //dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound);

        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);

        var topicName = AcmsContext.CurrentPost.Name;
        if (topicName == "Press-Releases") {
            topicLanguage="PressReleaseLanguage";
            topicProduct = "PressReleaseProduct";
        }
        else if (topicName == "Press-Clippings")
        {
            topicLanguage = "PressClippingsLanguage";
            topicProduct = "PressClippingsProduct";
        }
    }


    protected void Page_Load(object sender, EventArgs e)
    {
       pnlCheckAllCheckBoxList.Visible = false;
        if (!IsPostBack)
        {
            DataBind();
            pnlCheckAllCheckBoxList.Visible = true;
        }
    }

    private int linkTopicId;
    private Topic linkTopic;
    protected Topic LinkTopic
    {
        get
        {
            if (linkTopic == null && linkTopicId > 0)
            {
                linkTopic = Topic.GetById(linkTopicId);
            }
            return linkTopic;
        }
        set
        {
            if (value != null)
            {
                linkTopicId = value.Id;
            }
            linkTopic = value;
        }
    }

    void dlFeed_ItemDataBound(object sender, Acms.WebControls.AcmsDataListItemEventArgs e)
    {
        var post = e.Item.DataItem as Post;
        if (post != null)
        {
            var lblLanguage = e.Item.FindControl("lblLanguage") as Label;
            
            if (lblLanguage != null)
            {
                List<Topic> languages = null;
                
                if (AcmsContext.CurrentPost != null)
                {
                    languages = Topic.GetAllByPostIdAndTopicGroupPath(post.Id, topicLanguage);
                    //languages = Topic.GetAllByPostIdAndTopicGroupPath(AcmsContext.CurrentPost.Id, "PressReleaseLanguage");
                    
                }
                else
                {
                    languages = new List<Topic>();
                    while (languages.Count < 5)
                    {
                        languages.Add(Topic.CreateInstance());
                        languages.Last().Name = string.Format("Service Topic {0}", languages.Count());
                    }
                }
                
                if (languages.Count > 0)
                {
                    foreach (Topic t in languages) { 
                    lblLanguage.Text = t.Name;
                    }
                  
                }
                else
                {
                    lblLanguage.Text = "-";
                  
                }
                //Set <a> to download file.
                var lnkPdf = e.Item.FindControl("lnkPdf") as HyperLink;
                if (lnkPdf != null)
                {
                    var pdf = post.FindPostControl("PdfArticle");// as ImageContentControlDisplay;
                    var prop = pdf.GetProperties<Acms.AcmsControls.PdfContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        //var host = HttpContext.Current.Request.Url.Host;
                        //var requestUrl = Request.Url;
                        //var flvUrl = 
//                                            "http://" + HttpContext.Current.Request.Url.Host + ResolveUrl(Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, 500, null, this.Page));
                        lnkPdf.NavigateUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, null, 550, this.Page);//"http://" + HttpContext.Current.Request.Url.Host + ResolveUrl(Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page));
                        //lnkThumb.NavigateUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, null, 550, this.Page);
                        //lnkPdf.NavigateUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                        lnkPdf.Text = post.DisplayName;
                    }
                }
                //
            }
        }
    }
    
    int[] FilterIds;

    protected void btnApplyFilter_Click(object sender, EventArgs e)
    {
        List<int> languageFilterIds = new List<int>();

        List<Topic> languages = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, topicLanguage);
        for (int i = 0; i < cblLanguages.Items.Length; i++)
        {
            if (cblLanguages.Items[i].Checked)
            {
                languageFilterIds.Add(languages[i].Id);
            }
        }
        LanguageFilterIds = languageFilterIds.ToArray();

        List<int> productFilterIds = new List<int>();
        List<Topic> products = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, topicProduct);
        for (int i = 0; i < cblProducts.Items.Length; i++)
        {
            if (cblProducts.Items[i].Checked)
            {
                productFilterIds.Add(products[i].Id);
            }
        }
        ProductFilterIds = productFilterIds.ToArray();
        dlFeed.DataBind();
        //ucFeedModule.Rebind();

        //List<int> filters = new List<int>();

        //List<Topic> languages = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "PressReleaseLanguage");
        //for (int i = 0; i < cblLanguages.Items.Length; i++)
        //{
        //    if (cblLanguages.Items[i].Checked)
        //    {
        //        filters.Add(languages[i].Id);
        //    }
        //}
        //List<Topic> products = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.CurrentPost.SiteId, "PressReleaseProduct");
        //for (int i = 0; i < cblProducts.Items.Length; i++)
        //{
        //    if (cblProducts.Items[i].Checked)
        //    {
        //        filters.Add(products[i].Id);
        //    }
        //}
        //FilterIds = filters.ToArray();
        //dlFeed.DataBind();

    }
    int[] ProductFilterIds;
    int[] LanguageFilterIds;

    void dsList_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        List<Post> prodData = null;
        List<Post> languageData = null;

        if (ProductFilterIds != null && ProductFilterIds.Length > 0)
        {
            prodData = Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, ProductFilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        }
        if (LanguageFilterIds != null && LanguageFilterIds.Length > 0)
        {
            languageData = Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, LanguageFilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        }

        //if both need to find posts in both
        if (prodData != null && languageData != null)
        {
            var langPostIDs = languageData.Select(x => x.Id);
            var commonPosts = prodData.Where(x => langPostIDs.Contains(x.Id)).ToList();
            e.SetData(commonPosts);
        }
        //else if (prodData != null)
        //{
        //    e.SetData(prodData);
        //}
        //else if (languageData != null)
        //{
        //    e.SetData(languageData);
        //}
        else
        {

            if (txtFirstFilter.Value == "0")
            {
                e.SetData(AcmsContext.CurrentPost.Children);
                txtFirstFilter.Value = "1";
            }
            else
            {
                //e.SetData(null);
            }
        }

        
        //List<Post> data = null;
        //if (FilterIds == null || FilterIds.Length == 0)
        //{
        //    data = AcmsContext.CurrentPost.Children;
        //}
        //else if (FilterIds.Length == 1)
        //{
        //    data = Post.GetAllByTopicIdAndParentPath(AcmsContext.SiteId, FilterIds[0], AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        //}
        //else
        //{
        //    data = Post.GetAllByTopicIdsAndParentPath(AcmsContext.SiteId, FilterIds, AcmsContext.CurrentPost.Path, AcmsContext.SiteVersion);
        //}
        //e.SetData(data);

    }

    protected override void DataBindChildren()
    {
        cblProducts.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, topicProduct);
        cblLanguages.DataSource = Topic.GetAllBySiteIdAndTopicGroupPath(AcmsContext.SiteId, topicLanguage);
        base.DataBindChildren();
    }


    protected override object SaveViewState()
    {
        ViewState["ProductFilterIds"] = ProductFilterIds;
        ViewState["LanguageFilterIds"] = LanguageFilterIds;
        return base.SaveViewState();
    }

    protected override void LoadViewState(object savedState)
    {
        base.LoadViewState(savedState);
        ProductFilterIds = (int[])ViewState["ProductFilterIds"];
        LanguageFilterIds = (int[])ViewState["LanguageFilterIds"];
    }

    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }
    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
}
